
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <pwd.h>

#define MAXLINE 1024

void err_sys(char* msg) {
    printf("err_sys %s\n", msg );
    exit(0);
}

int main(void) {
    int     n;
    int     fd[2];
    pid_t   pid;
    char    line[MAXLINE];

    if (pipe(fd) < 0)
        err_sys("pipe error");
    if ((pid = fork()) < 0) {
        err_sys("fork error");
    } else if (pid > 0) {       /* parent */
        close(fd[0]);
        printf("parent write\n");
        write(fd[1], "hello world\n", 12);
    } else {                /* child */
        close(fd[1]);
        printf("child read\n");
        n = read(fd[0], line, MAXLINE);
        write(STDOUT_FILENO, line, n);
    }
    exit(0);
}


